(function() {

	function _isArray(obj) {
		return Object.prototype.toString.call(obj) === "[object Array]";
	}

	function _isString(obj) {
		return Object.prototype.toString.call(obj) === "[object String]";
	}

	function _genid(min, max) {
		return Math.floor(Math.random() * (max - min + 1) + min);
	}

	function _genderDom_Vue() {
		/*<div class="tree_container left_tree_container">
			<el-input placeholder="输入关键字进行过滤" v-model="leftFilterText" style="margin-bottom: 10px;">
			</el-input>
			<h1 v-text="leftTitle"></h1>
			<el-tree :style="styleObject" class="filter-tree" show-checkbox :data="leftTreeData" :props="defaultProps" default-expand-all :filter-node-method="leftFilterNode" ref="leftTree">
			</el-tree>
		</div>
		<div class="move_btns">
			<el-button class="loading" type="primary" @click="add($event)">分配<i class="el-icon-arrow-right el-icon--right"></i></el-button>
			<el-button class="loading" type="primary" icon="arrow-left" @click="remove($event)">移除</el-button>
		</div>
		<div class="tree_container right_tree_container">
			<el-input placeholder="输入关键字进行过滤" v-model="rightFilterText" style="margin-bottom: 10px;">
			</el-input>
			<h1 v-text="rightTitle"></h1>
			<el-tree :style="styleObject" class="filter-tree" show-checkbox :data="rightTreeData" :props="defaultProps" default-expand-all :filter-node-method="rightFilterNode" ref="rightTree">
			</el-tree>
		</div>*/
	}

	function _functionToString(fn) {
		return /\*(.*?)\*/g.exec(fn.toString().replace(/\s+/g, " "))[1].trim();
	}



	function VanyTransfer_Vue(setting) {
		var el = setting.el.split("#")[1];
		var defaultProps = {
			id: "functionCode",
			children: "childrenList",
			label: "functionNameCh",
			left: "noAllotFunction",
			right: "allotFunction"
		};
		el = document.getElementById(el);
		el.innerHTML = _functionToString(_genderDom_Vue);
		el.className = "vany_transfer";
		if(setting.keySetting){
			defaultProps.id = setting.keySetting.id;
			defaultProps.children = setting.keySetting.children;
			defaultProps.label = setting.keySetting.name;
			defaultProps.left = setting.keySetting.left;
			defaultProps.right = setting.keySetting.right;
		}
		var vanytransfer = new Vue({
			el: setting.el,
			data: function() {
				return {
					leftFilterText: "",
					leftTreeData: [],
					leftCheckedNodes: [],
					rightFilterText: "",
					rightTreeData: [],
					rightCheckedNodes: [],
					defaultProps: defaultProps,
					styleObject: {
						height: "auto"
					}
				}
			},
			computed: {
				leftTitle: function() {
					return "待分配";
					// TODO
					if(this.leftTreeData.length === 0) {
						return "待分配";
					} else {
						return "待分配(" + this.leftTreeData.length + ")";
					}
				},
				rightTitle: function() {
					return "已分配";
					// TODO
					if(this.rightTreeData.length === 0) {
						return "已分配";
					} else {
						return "已分配(" + this.rightTreeData.length + ")";
					}
				},
			},
			watch: {
				leftFilterText: function(val) {
					this.$refs.leftTree.filter(val);
				},
				rightFilterText: function(val) {
					this.$refs.rightTree.filter(val);
				}
			},
			methods: {
				leftFilterNode: function(value, data) {
					if(!value) return true;
					return data[this.defaultProps.label].indexOf(value) !== -1;
				},
				rightFilterNode: function(value, data) {
					if(!value) return true;
					return data[this.defaultProps.label].indexOf(value) !== -1;
				},
				getCheckedNodes: function(position) {
					if(position === "left"){
						this.leftCheckedNodes = this.$refs["leftTree"].getCheckedNodes(true);
					}else{
						this.rightCheckedNodes = this.$refs["rightTree"].getCheckedNodes(true);
					}
				},
				getAllRightNodeId_Add: function(){
					var self = this;
					var leftCheckedNodes = this.leftCheckedNodes.map(function(item){
						return item[self.defaultProps.id];
					});
					var rightUnCheckedNodes = [];
					this.digui(this.rightTreeData, this.defaultProps.id, this.defaultProps.children, rightUnCheckedNodes);
					/*for(var i=0; i<this.rightTreeData.length; i++){
						if(!this.rightTreeData[i][this.defaultProps.children]){
							rightCheckedNodes.push(this.rightTreeData[i][this.defaultProps.id]);
						}
					}*/
					return leftCheckedNodes.concat(rightUnCheckedNodes).join(",");
				},
				sendRightNodesid: function(url, params){
					VanyAjax(url, params).done(function(data){
						this.getData(setting.url, setting.params);
					}, true, this);
				},
				add: function(e) {
					this.getCheckedNodes("left");
					if(this.leftCheckedNodes.length === 0){
						this.$message({
							type: "warning",
							message: "请勾选要添加的节点"
						});
						e.stopPropagation();
						return false;
					}
					var params = {};
					for(var key in setting.params){
						params[key] = setting.params[key];
					}
					params[this.defaultProps.id + "s"] = this.getAllRightNodeId_Add();
					console.log(params);
					this.sendRightNodesid(setting.add.url, params);
				},
				getAllRightNodeId_Remove: function(){
					var rightUnCheckedNodes = [];
					var rightCheckedNodes = [];
					for(var i=0; i<this.rightCheckedNodes.length;i++){
						rightCheckedNodes.push(this.rightCheckedNodes[i][this.defaultProps.id]);
					}
					this.digui(this.rightTreeData, this.defaultProps.id, this.defaultProps.children, rightUnCheckedNodes);
					return this.removeArrayEl(rightUnCheckedNodes, rightCheckedNodes).join(",");
				},
				removeArrayEl: function(srcArr, toArr){
					var temp = [];
					for(var i=0; i<srcArr.length; i++){
						var flag = [];
						for(var j=0; j<toArr.length; j++){
							if(toArr[j] === srcArr[i]){
								flag.push(true);
							}else{
								flag.push(false);
							}
						}
						if(flag.indexOf(true) < 0){
							temp.push(srcArr[i]);
						}
					}
					return temp;
				},
				digui: function(data, idKey, childrenKey, resultArray){
					for(var i=0; i<data.length; i++){
						if(data[i][childrenKey]){
							this.digui(data[i][childrenKey], idKey, childrenKey, resultArray);
						}else{
							resultArray.push(data[i][idKey]);
						}
					}
				},
				unique: function(arr){
					var res = [];
					var json = {};
					for(var i=0; i<arr.length; i++){
						if(!json[arr[i]]){
							res.push(arr[i]);
							json[arr[i]] = 1;
						}
					}
					return res;
				},
				remove: function(e) {
					this.getCheckedNodes("right");
					if(this.rightCheckedNodes.length === 0){
						this.$message({
							type: "warning",
							message: "请勾选要移除的节点"
						});
						e.stopPropagation();
						return false;
					}
					var params = {};
					for(var key in setting.params){
						params[key] = setting.params[key];
					}
					params[this.defaultProps.id + "s"] = this.getAllRightNodeId_Remove();
					console.log(params);
					this.sendRightNodesid(setting.add.url, params);
				},
				getData: function(urlordata, params) {
					if(_isString(urlordata)) {
						VanyAjax(urlordata, params, "GET", setting.el).done(function(data){
							this.leftTreeData = data[this.defaultProps.left];
							this.rightTreeData = data[this.defaultProps.right];
						}, false, this);
					}
				},
				setTreeHeight: function(height) {
					this.styleObject.height = height + "px";
				}
			}
		});
		return vanytransfer
	}

	window.VanyTransfer = function(setting) {
		return new VanyTransfer_Vue(setting);
	}
})(window);